var __PLATFORM_SUPPORT_VERSION = 1.0;

if (!(typeof PlatformSupport == 'object' && PlatformSupport.version >= __PLATFORM_SUPPORT_VERSION)) {

    var PlatformSupport = {
        version: __PLATFORM_SUPPORT_VERSION,
        state: 'uninitialized'
    };

    var PlatformSupport_init = function () {

		/********** 各平台通用的支持API **********/
		var __PlatformSupport_Common = {

			listItems: function (callback) {
				if (!callback)
					return;

				var list = Storage.listRecords();
				callback(list);
			},

			getItem: function (key, callback) {
				if (!callback)
					return;

				var value = Storage.getRecord(key);
				callback(value);
			},

			writeItem: function (key, data, callback) {
				if (!callback)
					return;

				var obj = $.extend({}, data, { key: key });
				Storage.writeRecord(obj);
				callback(true);
			},

			deleteItem: function (key, callback) {
				if (!callback)
					return;

				Storage.deleteRecord(key);
				callback(true);
			},

			exit: function () {
				navigator.app.exitApp();
			},

			checkPassword: function (value, callback) {
				if (!callback)
					return;

				var real = Storage.getPassword();
				callback(real == value);
			},

			modifyPassword: function (value, callback) {
				Storage.setPassword(value);
				if (callback)
					callback();
			},

			isPasswordSet: Storage.isPasswordSet

		};

    	/********** 开发调试用的仿真 系统平台支持 **********/

    	var __PlatformSupport_FAKE = {

    		platform: '_FAKE',

    		version: __PLATFORM_SUPPORT_VERSION,

			exit: function () {
				alert('Will exit if on device.');
			}

    	};

    	/********** Android 系统平台支持 **********/

    	var __PlatformSupport_ANDROID = {

    		platform: 'Android',

    		version: __PLATFORM_SUPPORT_VERSION,

    	};

    	/********** IOS 系统平台支持 **********/

    	var __PlatformSupport_IOS = {

    		platform: 'IOS',

    		version: __PLATFORM_SUPPORT_VERSION,

		};

    	/********** 平台选择 **********/
    	var __PLATFORM_TYPE = '_FAKE'; // Android 系统则为 Android, IOS 系统则为 IOS
    	if (typeof device == 'object') {
    		if (device.platform == 'Android')
    			__PLATFORM_TYPE = 'Android';
    		else if (device.platform == 'iOS')
    			__PLATFORM_TYPE = 'IOS';
    	}

    	var obj = __PlatformSupport_Common;

    	if (__PLATFORM_TYPE == '_FAKE')
			obj = $.extend({}, obj, __PlatformSupport_FAKE);
		else if (__PLATFORM_TYPE == 'Android')
			obj = $.extend({}, obj, __PlatformSupport_ANDROID);
    	else if (__PLATFORM_TYPE == 'IOS')
			obj = $.extend({}, obj, __PlatformSupport_IOS);
		else
    		throw '不能够识别的操作系统平台: ' + __PLATFORM_TYPE;

    	/********** TODO: 清除掉不要的PlatformSupport对象 **********/

    	/********** 返回平台支持对象 **********/

    	PlatformSupport = obj;
    };

}